define(['jquery', 'bootstrap', 'frontend', 'form', 'table', 'template'], function ($, undefined, Frontend, Form, Table, Template) {
    var validatoroptions = {
        invalid: function (form, errors) {
            $.each(errors, function (i, j) {
                Layer.msg(j);
            });
        }
    };
    var Controller = {
        index: function () {
            setInterval(function () {
                window.location.reload();
            }, 6000000);//100分钟刷新一次

            Controller.api.bindevent();
        },
        addmaintenance: function () {

            Controller.api.bindevent();

            Form.api.bindevent($("form#form2"), function (data, ret) {
                //console.log(ret);
                setTimeout(function () {
                    location.href = ret.url ? ret.url : "/";
                }, 1000);
            });

        },
        newtask: function () {
            Controller.api.bindevent();
        },
        updatetask: function () {
            Controller.api.bindevent();
        },
        anchanglist: function () {
            Controller.api.bindevent(); //不加这段，cxselect不能用，加了form不能用get，只能用post
        },
        cbzy: function () {
            Controller.api.bindevent();

        },
        cbzy1: function () {

            Controller.api.bindevent();
        },
        cbzy2: function () {

            Controller.api.bindevent();

            $("#c-submit").click(function (e) {
                var formdata = $("#add-form").serialize();
                var x = $("#add-form").serializeArray();
                var row = new Array();
                var ispreprocessing = false;
                $.each(x, function (i, field) {
                    row[field.name] = field.value;
                });
                console.log(formdata);
                var url = 'cbqr?sdbiao_id=' + row["sdbiao_id"] + "&cbs=" + row["cbs"];
                location.href = url;
            });
        },
        cbqr: function () {
            Controller.api.bindevent();
        },
        searchanchang: function () {
            Controller.api.bindevent();
        },
        maintenancelog: function () {
            Controller.api.bindevent();
        },
        tasklist: function () {

        },
        iot: function () {
            Controller.api.bindevent();
        },
        sgwb: function () {
            var lng = 0;
            var lat = 0;
            require(['async!BMap'], function () {
                var geolocation = new BMap.Geolocation();
                geolocation.getCurrentPosition(function (r) {
                    if (this.getStatus() == BMAP_STATUS_SUCCESS) {
                        console.log('您的位置：' + r.point.lng + ',' + r.point.lat);
                        lng = r.point.lng;
                        lat = r.point.lat;
                    } else {
                        //Layer.alert('failed' + this.getStatus());
                    }
                }, {enableHighAccuracy: true});
            });

            $(document).on("click", "#jiean", function () {
                var url = 'home/jiean';
                var _this = $(this);
                Fast.api.ajax({
                    url: url,
                    data: {lng: lng, lat: lat, task_id: Config.task_id}
                }, function (data, ret) {
                    setTimeout(function () {
                        location.href = ret.url ? ret.url : "/";
                    }, 1000);
                });
            });

            $(document).on("click", "#dbj", function () {
                var url = 'home/dbj';
                var _this = $(this);
                Fast.api.ajax({
                    url: url,
                    data: {lng: lng, lat: lat, task_id: Config.task_id}
                }, function (data, ret) {
                    setTimeout(function () {
                        //window.history.back();
                        location.href = "updatetask?ids=" + Config.task_id;
                    }, 1000);
                });
            });

            $(document).on("click", "#dqk", function () {
                var url = 'home/dqk';
                var _this = $(this);
                Fast.api.ajax({
                    url: url,
                    data: {lng: lng, lat: lat, task_id: Config.task_id}
                }, function (data, ret) {
                    setTimeout(function () {
                        location.href = "updatetask?ids=" + Config.task_id;
                    }, 1000);
                });
            });

            $(document).on("click", "#hxsg", function () {
                var url = 'home/hxsg';
                var _this = $(this);
                Fast.api.ajax({
                    url: url,
                    data: {lng: lng, lat: lat, task_id: Config.task_id}
                }, function (data, ret) {
                    setTimeout(function () {
                        location.href = "updatetask?ids=" + Config.task_id;
                    }, 1000);
                });
            });

        },
        task: function () {

            var lng = 0;
            var lat = 0;
            require(['async!BMap'], function () {
                var geolocation = new BMap.Geolocation();
                geolocation.getCurrentPosition(function (r) {
                    if (this.getStatus() == BMAP_STATUS_SUCCESS) {
                        console.log('您的位置：' + r.point.lng + ',' + r.point.lat);
                        lng = r.point.lng;
                        lat = r.point.lat;
                    } else {
                        //Layer.alert('failed' + this.getStatus());
                    }
                }, {enableHighAccuracy: true});
            });

            $(document).on("click", "#kssg", function () {
                var kssg = $("#kssg").html();
                if (kssg == "開始施工") {
                    var url = 'home/kssg';
                    var _this = $(this);
                    Fast.api.ajax({
                        url: url,
                        data: {lng: lng, lat: lat, task_id: Config.task_id}
                    }, function (data, ret) {
                        //Backend.api.toastr.success('開始施工！');
                        $("#kssg").html("正在施工");
                        $("#kssg").attr("style", "opacity: 0.2");
                    });
                }
            });

            var kssg = $("#kssg").html();
            if (kssg == "正在施工") {
                $("#kssg").attr("style", "opacity: 0.2");
            }

            $(document).on("click", "#bjdqr", function () {
                var url = 'home/bjdqr';
                var _this = $(this);
                Fast.api.ajax({
                    url: url,
                    data: {lng: lng, lat: lat, task_id: Config.task_id}
                }, function (data, ret) {
                    //Backend.api.toastr.success('開始施工！');
                });
            });
            //$(document).on("click", "#sgwb", function () {
            //    var url = 'home/sgwb';
            //    var _this = $(this);
            //    Fast.api.ajax({
            //        url: url,
            //        data: {lng: lng, lat: lat, task_id: Config.task_id}
            //    }, function (data, ret) {
            //        //Backend.api.toastr.success('開始施工！');
            //    });
            //});
            $(document).on("click", "#qkdsd", function () {
                var url = 'home/qkdsd';
                var _this = $(this);
                Fast.api.ajax({
                    url: url,
                    data: {lng: lng, lat: lat, task_id: Config.task_id}
                }, function (data, ret) {
                    //Backend.api.toastr.success('開始施工！');
                });
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"), function (data, ret) {
                    //console.log(ret);
                    setTimeout(function () {
                        location.href = ret.url ? ret.url : "/";
                    }, 1000);
                });
            },
            formatter: {//渲染的方法
                zxstatus: function (value, row, index) {
                    //Controller.api.formatter.zxstatus
                    if (value == 0) {
                        return [
                            '<a href="javascript:;" class="bg-red-gradient"> 離線 </a>'
                        ].join("")

                    } else {
                        return [
                            '<a href="javascript:;" class="bg-green"> 在線 </a>'
                        ].join("")
                    }
                },
                onlinetime: function (value, row, index) {
                    var online_starttime = row.online_starttime;
                    var nowtime = new Date().getTime() / 1000;
                    console.log(online_starttime);

                    if (online_starttime > 0) {
                        var s = nowtime - online_starttime;
                        var hour = Math.floor(s / 3600);
                        var min = Math.floor((s - hour * 3600) / 60);
                        var sec = Math.floor(s % 60);
                        var str = hour + " : " + min + " : " + sec;
                        return [
                            '<a href="javascript:;"> ' + str + ' </a>'
                        ].join("")
                    } else {
                        return [
                            '<a href="javascript:;"> -- </a>'
                        ].join("")
                    }
                }
            }
        }
    };
    return Controller;
});